﻿App.Sendbill = function() {
    return {

        currentFormValues: {},

        getStore: function() {
            var store = new Ext.data.Store({
                autoLoad:true,
				baseParams : {start:0, limit:25},
				proxy: new Ext.data.HttpProxy({
					url : 'cost/sendbill_list'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'object_id',
					fields: [{
						name: 'object_id',
						type: 'string'
					},
					{
						name:'cost_id'
					},{
						name: 'objid',
						type: 'string'
					},
					{
						name:'obj_name',
						type:'string'
					},
					{
						name:'obj_num',
						type:'string'
					},
					{
						name: 'from_addr',
						allowBlank: false
					},
					{
						name: 'to_addr',
						allowBlank: false
					},
					{
						name:"cost_id"
					},
					{
						name:"cost_type"
					},
					{
						name:"sum_fee"
					}]
				})
            });
            return store;
        },
       createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";

            var sm = new Ext.grid.CheckboxSelectionModel();
            this.grid = new Ext.grid.GridPanel({
            	anchor:'top 50%',
                loadMask: true,
                stripeRows: true,
                tbar: new Ext.PagingToolbar({ 
                	pageSize: 20 ,//分页必须加的参数.最好和store里面的limit保持一致
                	displayInfo: true,
                    store: this.store,
                    items:[
                    {
	                    text: '下账',
	                    scope: this,
	                    handler: this.del
	                },
	                '-', new Ext.ux.form.SearchField({
                    store: this.store,
                    width: 110
                })]}),
                title:"下账管理",//打印必须设置tile
                plugins: [Ext.ux.plugins.Print],//打印按钮
                loadMask: true,
                viewConfig : {autoFill:false,forceFit:true},
                autoHeight:true,
                store: this.store,
                sm: sm,
                columns: [sm,new Ext.grid.RowNumberer(), 
              {
                	header:"账号",
                	dataIndex:'cost_id',
                	hidden:true
    	
              },{
                    header: "货号",
                    width: 60,
                    sortable: true,
                    dataIndex: 'objid',
                    renderer: this.highLight
                },
               {
                    header: "货名",
                    width: 100,
                    sortable: true,
                    dataIndex: 'obj_name',
                    renderer: this.highLight
                },
                {
                    header: "件数",
                    width: 100,
                    sortable: true,
                    dataIndex: 'obj_num',
                    renderer: this.highLight
                },
               /* {
                    header: "收货人",
                    width: 100,
                    sortable: true,
                    dataIndex: 'to_name',
                    renderer: this.highLight
                },
               */ {
                	header: "付款方式",
                    width: 150,
                    sortable: true,
                    dataIndex: 'cost_type'
                },
                {
                    header: "总费用",
                    width: 150,
                    sortable: true,
                    dataIndex: 'sum_fee'
                }]
            });
            panel.add(this.grid);

        },
        del: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                var recs = this.grid.getSelectionModel().getSelections();
                var ids = [];
                var titles = '';
                for (var i = 0; i < recs.length; i++) {
                    var data = recs[i].data;
                    ids.push(data.cost_id);
                    titles += data.objid +'  '+data.sum_fee+ '<br>';
                }
                Ext.Msg.confirm('下账', '确认以下货款下账？<br><font color="red">' + titles + '</font>',
                function(btn) {
                    if (btn == 'yes') {
						Ext.Ajax.request({
							method:'post',
							url:'cost/sendbill_done',
							params:{
								ids:ids.toString()
							},
							success: function(resp, opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								if(result.success=='true') {
									Ext.Msg.alert('信息', info);
                                    App.Sendbill.store.reload();
								}else {
                                    Ext.Msg.alert('信息', info);
                                }
							},
							failure: function(resp,opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								Ext.Msg.alert('提示', info); 
							}
						});
                    }
                });
            } else {
                Ext.Msg.alert('信息', '请选择要下账的货号！');
            }
        },


        render: function(id) {
            if (!this.store) {
                this.store = this.getStore();
            };
    //        if (!this.form) {
   //             this.form = this.getForm();
   //         };
    //        if (!this.dlg) {
   //             this.dlg = this.getDialog();
   //         };
            this.createGrid(id);

        }
    };
} ();
